package org.mozhu.file.manager.mount;

import java.util.HashMap;
import java.util.Map;

public class MountDirectories {

	private Map<String, MountMeta> mountMetas = new HashMap<>();

	public void mount(String mountPath, String realPath) {
		mountPath = mountPath.replace("/", "");
		mountMetas.put(mountPath, new MountMeta(mountPath, realPath));
	}

	public void unmount(String mountPath) {
		mountMetas.remove(mountPath);
	}

	public Map<String, MountMeta> getMountDirectories() {
		return mountMetas;
	}

	public MountDirectory getMountDirectory(String virtualPath) {
		virtualPath = cleanPath(virtualPath);
		for (MountMeta mountMeta : mountMetas.values()) {
			if (mountMeta.isMatchMountPath(virtualPath)) {
				return new MountDirectory(mountMeta).setVirtualPath(virtualPath);
			}
		}
		throw new IllegalStateException("No match mount directory found for path[" + virtualPath + "]");
	}
	
	public String cleanPath(String path){
		return path.replaceAll("[/]{2,}","/");
	}

}
